package com.xust.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xust.oa.domain.dto.DepartmentDTO;
import com.xust.oa.domain.entity.TDepartment;
import com.xust.oa.domain.vo.DepartmentVO;
import com.xust.oa.service.TDepartmentService;
import com.xust.oa.utils.Result;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zj
 * @time 2025/3/25 16:02
 */
@RestController
@RequestMapping("/dept")
public class DeptController {



    @Autowired
    TDepartmentService departmentService;


    @PostMapping("getList")
    public Result<List<DepartmentVO>> getDeptList(@RequestBody DepartmentDTO departmentDTO){
        return departmentService.searchList(departmentDTO);
    }

    @GetMapping("/count")
    public Result<Long> getCount(DepartmentDTO departmentDTO){

        QueryWrapper<TDepartment> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(departmentDTO.getName()))
            queryWrapper.like("name", departmentDTO.getName());
        if (!StringUtils.isEmpty(departmentDTO.getEnabled()))
            queryWrapper.eq("enabled", departmentDTO.getEnabled());
        long count = departmentService.count(queryWrapper);
        return Result.ok(count);
    }

    @GetMapping("/list")
    public Result<List<String>> getAllDept(){
        List<String> list = departmentService.list().stream().map(TDepartment::getName).collect(Collectors.toList());
        return Result.ok(list);
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        try {
           departmentService.removeById(id);

            QueryWrapper<TDepartment> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id",id);
            departmentService.remove(queryWrapper);
           return Result.ok("删除成功");
        }catch (Exception e){
            return Result.buildFailure(400,"删除失败");
        }
    }

    @PostMapping("/add")
    public Result add(@RequestBody TDepartment departmentDTO){
        if (departmentDTO == null||departmentDTO.getName()==null) return Result.buildFailure(400,"参数错误");
        departmentDTO.setId(null);
        try {
            if (departmentDTO.getParentId()==null)departmentDTO.setParentId(-1);
            departmentService.save(departmentDTO);
            return Result.ok("添加成功");
        }catch (Exception e){
            return Result.buildFailure(400,"添加失败,请检查部门名参数是否重复");
        }
    }
    @PostMapping("/update")
    public Result update(@RequestBody TDepartment departmentDTO){
        if (departmentDTO == null||departmentDTO.getName()==null) return Result.buildFailure(400,"参数错误");
        try {
            departmentService.updateById(departmentDTO);
            return Result.ok("修改成功");
        }catch (Exception e){
            return Result.buildFailure(400,"修改失败,请检查部门名参数是否重复");
        }
    }

}
